﻿Partial Public Class WebForm_ManageDescriptionUpload
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            '判斷是否有找到
            If DirectCast(Master.FindControl("lblHeadText"), Label) IsNot Nothing Then
                DirectCast(Master.FindControl("lblHeadText"), Label).Text = Me.GetLocalResourceObject("Page.Text")
            End If
        End If
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim hlinkValue As HyperLink = e.Row.FindControl("hlinkValue")
            Dim FilePath As String = ""
            If hlinkValue IsNot Nothing Then
                '編輯
                hlinkValue.Text &= ".doc"
                FilePath = Server.MapPath(System.Web.Configuration.WebConfigurationManager.AppSettings("DescriptionPath")) & hlinkValue.Text

                If System.IO.File.Exists(FilePath) Then
                    hlinkValue.NavigateUrl = Me.ResolveUrl(System.Web.Configuration.WebConfigurationManager.AppSettings("DescriptionPath")) & hlinkValue.Text
                Else
                    hlinkValue.Enabled = False
                End If
            End If
        End If
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim b1 As Button = CType(sender, Button)
        Dim cell1 As DataControlFieldCell = CType(b1.Parent, DataControlFieldCell)
        Dim row1 As GridViewRow = CType(cell1.Parent, GridViewRow)
        Dim hiFileUpload As FileUpload = gv.Rows(row1.RowIndex).FindControl("hiFileUpload")
        Dim hlinkValue As HyperLink = gv.Rows(row1.RowIndex).FindControl("hlinkValue")
        Dim FilePath As String = ""
        If Not hiFileUpload.HasFile Then
            MyObj.ShowMessage("此上傳檔案不存在!!")
            Return
        ElseIf hiFileUpload.PostedFile.ContentType.ToString().ToUpper() <> "APPLICATION/MSWORD" Then
            MyObj.ShowMessage("上傳失敗!!請選擇Doc檔案!!")
            Return
        Else
            Try
                FilePath = Server.MapPath(System.Web.Configuration.WebConfigurationManager.AppSettings("DescriptionPath")) & hlinkValue.Text
                hiFileUpload.SaveAs(FilePath)
                gv.EditIndex = -1
            Catch ex As Exception
                MyObj.ShowMessage("上傳失敗!!")
                MyObj.WriteLog(ex.ToString())
                Return
            End Try
        End If
    End Sub
End Class
